using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace VPlayer{ public partial class Form_Main : Form { #region 变量 VlcPlayer vlc_player; bool IsLocal = false;//本地播还是网络播 string PlaySource = string.Empty;//播放源(本地/URL) double PlayPos = 0;//播放位置(=-1时用播放,>=0时用暂停) #endregion #region 构造 public Form_Main() { InitializeComponent(); string pluginPath = System.Environment.CurrentDirectory "\\plugins\\"; vlc_player = new VlcPlayer(pluginPath); IntPtr render_wnd = panelMain.Handle; vlc_player.SetRenderWindow((int)render_wnd); } #endregion #region 方法 private void Init() { tsl_Version.Text = string.Format(" 版本:{0}", System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString()); } private void BindTree() { List<string> listc = CommonLib.GetTvClass(); List<TvInfo> list = CommonLib.GetTvList(); foreach (var c in listc) { TreeNode node1 = treeView.Nodes.Add(c); var lst = list.FindAll(x => x.TvClassName == c); foreach (var item in lst) { TreeNode node2 = new TreeNode(); node2.Text = item.TvName; node2.Tag = item.TvUrl; node1.Nodes.Add(node2); } } treeView.ExpandAll(); } private void BindTree2() { List<string> listc = CommonLib.GetTvClass(); List<TvInfo> list = CommonLib.GetTvList(); TreeNode node1 = treeView.Nodes.Add("央视"); foreach (var item in list) { TreeNode node2 = new TreeNode(); node2.Text = item.TvName; node2.Tag = item.TvUrl; node1.Nodes.Add(node2); } treeView.ExpandAll(); } /// <summary> /// 播放URL(弹出对话框) /// </summary> public void PlayUrl(string Url) { //string url = "http://58.135.196.138:8090/live/a9a97bed07eca008a1c88c9d7c74965d/index.m3u8"; vlc_player.PlayUrl(Url); tsb_Play.Image = Properties.Resources.pause;//播放变成暂停 tsb_Play.Text = "暂停"; IsLocal = false; PlaySource = Url; timer.Enabled = false; panelProgress.Visible = false; } #endregion #region 事件 /// <summary> /// 加载 /// </summary> private void Form_Main_Load(object sender, EventArgs e) { Init(); BindTree(); } /// <summary> /// 播放 /// </summary> private void tsb_Play_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(PlaySource))//默认打开本地文件 { MenuItemOpenFile_Click(null, null); return; } if (tsb_Play.Text == "暂停")//播放状态 { vlc_player.Pause(); tsb_Play.Image = Properties.Resources.play; tsb_Play.Text = "播放"; if (IsLocal)//本地播放才有播放位置 { PlayPos = vlc_player.GetPlayTime(); timer.Stop(); } } else { if (IsLocal) { if (PlayPos == -1) { vlc_player.PlayFile(PlaySource); } else { vlc_player.Pause(); } //vlc_player.SetPlayTime(PlayPos); tsb_Play.Image = Properties.Resources.pause; tsb_Play.Text = "暂停"; timer.Start(); } else { if (PlayPos == -1) { vlc_player.PlayUrl(PlaySource); } else { vlc_player.Pause(); } tsb_Play.Image = Properties.Resources.pause; tsb_Play.Text = "暂停"; } } } /// <summary> /// 停止 /// </summary> private void tsb_Stop_Click(object sender, EventArgs e) { vlc_player.Stop(); tsb_Play.Image = Properties.Resources.play; tsb_Play.Text = "播放"; PlayPos = -1; if (IsLocal) { timer.Stop(); progress.Value = 0; lbTime.Text = ""; } } /// <summary> /// 最大化 /// </summary> private void tsb_Max_Click(object sender, EventArgs e) { if (this.FormBorderStyle == FormBorderStyle.None)//已经全屏 { this.FormBorderStyle = FormBorderStyle.Sizable; this.WindowState = FormWindowState.Normal; vlc_player.SetFullScreen(false); tsb_Max.Image = Properties.Resources.enlarge; tsb_Max.Text = "最大化"; if (tsb_List.Text == "隐藏列表") { treeView.Visible = true; } else { treeView.Visible = false; } } else { this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; vlc_player.SetFullScreen(true); tsb_Max.Image = Properties.Resources.decrease; tsb_Max.Text = "退出最大化"; treeView.Visible = false; } } /// <summary> /// 显示隐藏列表 /// </summary> private void tsb_List_Click(object sender, EventArgs e) { if (tsb_List.Text == "隐藏列表") { tsb_List.Text = "显示列表"; treeView.Visible = false; } else { tsb_List.Text = "隐藏列表"; treeView.Visible = true; } } /// <summary> /// ESC键盘退出全屏 /// </summary> private void Form_Main_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape)//ESC键盘退出全屏 { if (this.FormBorderStyle == FormBorderStyle.None)//已经全屏 { tsb_Max_Click(null, null); } } } private void Form_Main_Resize(object sender, EventArgs e) { } /// <summary> /// 播放直播 /// </summary> private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node == null || e.Node.Tag == null) { PlaySource = ""; return; } string url = e.Node.Tag.ToString(); if (!string.IsNullOrEmpty(url)) { //vlc_player.Stop(); vlc_player.PlayUrl(url); tsb_Play.Image = Properties.Resources.pause;//播放变成暂停 tsb_Play.Text = "暂停"; IsLocal = false; PlaySource = url; timer.Enabled = false; panelProgress.Visible = false; } } #endregion #region 右键菜单 private void contextMenu_Opening(object sender, CancelEventArgs e) { if (!string.IsNullOrEmpty(PlaySource)) { MenuItemSnopShot.Enabled = true; MenuItemPlay.Enabled = true; MenuItemStop.Enabled = true; } else { MenuItemSnopShot.Enabled = false; MenuItemPlay.Enabled = false; MenuItemStop.Enabled = false; } } /// <summary> /// 播放本地文件 /// </summary> private void MenuItemOpenFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { vlc_player.PlayFile(ofd.FileName); progress.Maximum = (int)vlc_player.Duration(); progress.Value = 0; tsb_Play.Image = Properties.Resources.pause; tsb_Play.Text = "暂停"; IsLocal = true; PlaySource = ofd.FileName; timer.Enabled = true; panelProgress.Visible = true; } } /// <summary> /// 播放直播 /// </summary> private void MenuItemOpenUrl_Click(object sender, EventArgs e) { Form_Url form = new Form_Url(); form.PlayUrlDelegate = PlayUrl; form.ShowDialog(); } /// <summary> /// 前端显示 /// </summary> private void MenuItemTop_Click(object sender, EventArgs e) { if (MenuItemTop.Checked == false) { MenuItemTop.Checked = true; this.TopMost = true; } else { MenuItemTop.Checked = false; this.TopMost = false; } } /// <summary> /// 静音 /// </summary> private void MenuItemMute_Click(object sender, EventArgs e) { if (vlc_player.GetVolume() > 0) { vlc_player.SetVolume(0); MenuItemMute.Checked = true; } else { vlc_player.SetVolume(100); MenuItemMute.Checked = false; } } /// <summary> /// 立体声 /// </summary> private void MenuItemSoundStereo_Click(object sender, EventArgs e) { } /// <summary> /// 左声道 /// </summary> private void MenuItemSoundLeft_Click(object sender, EventArgs e) { } /// <summary> /// 右声道 /// </summary> private void MenuItemSoundRight_Click(object sender, EventArgs e) { } /// <summary> /// 全屏 /// </summary> private void MenuItemFullScreen_Click(object sender, EventArgs e) { tsb_Max_Click(null, null); } /// <summary> /// 截图 /// </summary> private void MenuItemSnopShot_Click(object sender, EventArgs e) { SaveFileDialog d = new SaveFileDialog(); d.Filter = "png files(*.png)|*.png"; d.RestoreDirectory = true; if (d.ShowDialog() == DialogResult.OK) { vlc_player.Take_SnopShot(d.FileName); } } /// <summary> /// 关于 /// </summary> private void MenuItemAbout_Click(object sender, EventArgs e) { string msg = string.Format("VPlayer!\r\nVersion:{0}\r\nQQ:19995488(吉米)", System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString()); MessageBox.Show(msg, "关于", MessageBoxButtons.OK, MessageBoxIcon.Information); } /// <summary> /// 播放 /// </summary> private void MenuItemPlay_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(PlaySource)) { tsb_Play_Click(null, null); } } /// <summary> /// 停止 /// </summary> private void MenuItemStop_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(PlaySource)) { tsb_Stop_Click(null, null); } } #endregion #region 进度(本地播) /// <summary> /// 播放进度 /// </summary> private void timer_Tick(object sender, EventArgs e) { if (IsLocal == false) { timer.Enabled = false; return; } if (progress.Value == progress.Maximum) { vlc_player.Stop(); timer.Stop(); tsb_Play.Image = Properties.Resources.play; tsb_Play.Text = "播放"; PlayPos = -1; progress.Value = 0; lbTime.Text = ""; } else { progress.Value = progress.Value 1; lbTime.Text = string.Format("{0}/{1}", CommonLib.Int2Time(progress.Value), CommonLib.Int2Time(progress.Maximum)); } } /// <summary> /// 设置播放位置 /// </summary> private void progress_MouseClick(object sender, MouseEventArgs e) { if (IsLocal == false) return; int pos = e.X * progress.Maximum / progress.Width; progress.Value = pos; vlc_player.SetPlayTime(pos); } #endregion }}
评论